const url = 'https://www.ysq89.top';
// const url = 'https://www.hiphantom.com';

function login(user_id, f) {

  console.info("调用登录接口")

  if (!user_id) {
    var code;
    wx.showLoading();
    wx.login({
      success: function (res) {
        console.log(res)
        code = res.code; //获取code0
        // console.log('设置缓存code：' + res.code)
        wx.setStorageSync('code', res.code)
        wx.getUserInfo({
          success: function (res) {
            console.log(res);

            var UserInfo = res.rawData
            var encryptedData = res.encryptedData;
            var iv = res.iv;

            // console.log('设置缓存user_info：' + UserInfo)
            wx.setStorageSync('user_info', res.rawData)

            wx.request({
              url: url+'/Weixin/login',
              data: {
                user: UserInfo,
                code: code,
                encryptedData: encryptedData,
                iv: iv
              },
              success: function (res) {
                console.log(res.data);
                wx.hideLoading() //隐藏提示框
                console.group("缓存登录信息");

                
                console.log('缓存user：' + JSON.stringify(res.data.jmdata) );
                wx.setStorageSync('user', JSON.stringify(res.data.jmdata) );

                console.log('缓存session_key：' + res.data.session_key);
                wx.setStorageSync('session_key', res.data.session_key);

                console.log('缓存user_id：' + res.data.user_id)
                wx.setStorageSync('user_id', res.data.user_id);
                user_id = res.data.user_id;

                console.log('缓存openid：' + res.data.openid)
                wx.setStorageSync('openid', res.data.openid)

                console.log('缓存unionid：' + res.data.openid)
                wx.setStorageSync('unionid', res.data.unionid)

                console.groupEnd();
                f();
              }
            })
          },
          fail: function (res) {
            console.log(res)
            wx.hideLoading()
          }
        })
      }
    })

  }

}


// 手机号解密
function decrypt(data, f) {
  wx.request({
    url: url + 'Weixin/decrypt',
    data: data,
    success: function (res) {
      console.log(res.data);
      console.log('缓存token：' + res.data.token);
      wx.setStorageSync('token', res.data.token);
      f(res.data);
    }
  })
}

//用户登录
function user_login(data, f) {
  wx.request({
    url: url + 'Index/user_login',
    data: data,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'token': wx.getStorageSync('token'),
    },
    method: "POST",
    success: function (res) {
      console.log(res.data);
      console.log('缓存token：' + res.data.token);
      wx.setStorageSync('token', res.data.token);
      f(res.data);
    }
  })
}




function Public(address) {
  return function (data,f){
    wx.request({
      url: url + address,
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token'),
      },
      method:"POST",
      success: function (res) {
        console.log(res.data);
        if (res.data.status == -100){
          wx.removeStorage('token');
          wx.navigateTo({
            url: "../login/login"
          })
          return false;
        }
        
        if (res.statusCode != 200 || data.interface_error) {
          interface_error({
            method: "POST",
            url: url + address,
            data: JSON.stringify(data),
            code: res.statusCode,
          })
        }

        f(res.data);
      }
    })
  }
}



function interface_error(data) {
  console.log(data)
  wx.request({
    url: 'https://1haoshifu.com/api.php/Interface/put',
    data: data,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: "POST",
    success: function (res) {
      console.log(res)
      console.error("接口报错详情：", 'https://1haoshifu.com/api.php/Interface/index?id=' + res.data.id);
    },


  })
}







module.exports = {

  login,
  decrypt,
  user_login,
  url,



  client_propertyBill_getNotPayByRoom: Public("/client/propertyBill/getNotPayByRoom"),
  client_propertyBill_payment: Public("/client/propertyBill/payment"),
  client_propertyBill_pay: Public("/client/propertyBill/pay"),

  
  
  client_user_checkIn: Public("/client/user/checkIn"),
  client_user_getWxInfo: Public("/client/user/getWxInfo"),
  client_user_phone: Public("/client/user/phone"),
  client_user_getAnnouncementDes: Public("/client/user/getAnnouncementDes"),


  client_repair_getRepairCategory: Public("/client/repair/getRepairCategory"),
  client_repair_add: Public("/client/repair/add"),
  client_repair_getRepairs: Public("/client/repair/getRepairs"),
  client_repair_getRepairDes: Public("/client/repair/getRepairDes"),
  client_repair_urgeDeal: Public("/client/repair/urgeDeal"),
  client_repair_ownDeal: Public("/client/repair/ownDeal"),
  client_repair_pay: Public("/client/repair/pay"),


  


  client_community_getAllBindRoom: Public("/client/community/getAllBindRoom"),
  client_community_getMrRoom: Public("/client/community/getMrRoom"),


  client_user_getUserPhone: Public("/client/user/getUserPhone"),
  client_user_updateNickName: Public("/client/user/updateNickName"),
  client_user_getPhoneCode: Public("/client/user/getPhoneCode"),
  client_user_updatePhone: Public("/client/user/updatePhone"),
  client_user_getUserCenter: Public("/client/user/getUserCenter"),
  client_user_info: Public("/client/user/info"),


  clientPublic_getIndexAnnouncementDes: Public("/clientPublic/getIndexAnnouncementDes"),

  
  
  clientPublic_info: Public("/clientPublic/info"),


  client_parkingFee_query: Public("/client/parkingFee/query"),
  client_parkingFee_getMyCars: Public("/client/parkingFee/getMyCars"),
  client_parkingFee_getRechargeRecord: Public("/client/parkingFee/getRechargeRecord"),
  client_parkingFee_verPhoneCode: Public("/client/parkingFee/verPhoneCode"),
  client_parkingFee_getParkingFeeProjects: Public("/client/parkingFee/getParkingFeeProjects"),
  client_parkingFee_apply: Public("/client/parkingFee/apply"),

  client_parkingFee_pay: Public("/client/parkingFee/pay"),
  client_parkingFee_renewalFee: Public("/client/parkingFee/renewalFee"),
  client_parkingFee_getElectronicReceipt: Public("/client/parkingFee/getElectronicReceipt"),
  client_parkingFee_getRechargeRecordByCommunity: Public("/client/parkingFee/getRechargeRecordByCommunity"),
  

  client_propertyBill_getElectronicReceipt: Public("/client/propertyBill/getElectronicReceipt"),
  client_propertyBill_getPropertyBillRechargeSet: Public("/client/propertyBill/getPropertyBillRechargeSet"),
  client_propertyBill_payPropertyBillRecharge: Public("/client/propertyBill/payPropertyBillRecharge"),
  client_propertyBill_getPropertyBillRecharges: Public("/client/propertyBill/getPropertyBillRecharges"),
  client_propertyBill_getPropertyBillRechargeCount: Public("/client/propertyBill/getPropertyBillRechargeCount"),
  client_propertyBill_getPropertyBillRechargeShowData: Public("/client/propertyBill/getPropertyBillRechargeShowData"),



  client_complaintSuggest_add: Public("/client/complaintSuggest/add"),
  client_complaintSuggest_getComplaintSuggests: Public("/client/complaintSuggest/getComplaintSuggests"),
  client_complaintSuggest_getComplaintSuggestDes: Public("/client/complaintSuggest/getComplaintSuggestDes"),
  client_complaintSuggest_getComplaintSuggestCategory: Public("/client/complaintSuggest/getComplaintSuggestCategory"),
  client_complaintSuggest_ownDeal: Public("/client/complaintSuggest/ownDeal"),


  client_invoice_openInvoice: Public("/client/invoice/openInvoice"),
  
  client_invoice_wyf_sjfp: Public("/client/invoice/wyf_sjfp"),
  client_invoice_wyf_sjfp_ycsdf_sjfp: Public("/client/invoice/wyf_sjfp_ycsdf_sjfp"),
  client_invoice_wyf_sjfp_lssf_sjfp: Public("/client/invoice/wyf_sjfp_lssf_sjfp"),
  client_invoice_wyf_sjfp_tcf_sjfp: Public("/client/invoice/wyf_sjfp_tcf_sjfp"),
  client_invoice_wyf_sjfp_ycwyf_sjfp: Public("/client/invoice/wyf_sjfp_ycwyf_sjfp"),
  client_invoice_findInvoice: Public("/client/invoice/findInvoice"),


  client_invoice_isWeChatCardAuth: Public("/client/invoice/isWeChatCardAuth"),
  client_invoice_weChatCardAuth: Public("/client/invoice/weChatCardAuth"),


  // /client/propertyBill/getPropertyBillRechargeShowData
  // /client/propertyBill/payment


}
